package com.blue.rpc.client;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import com.blue.core.util.UUID;
import com.blue.rpc.core.message.RpcRequest;

/**
 * 动态代理
 * 
 * @author zhengj
 * @since 1.0 2016年10月13日
 */
public class RpcInvocationHandler implements InvocationHandler
{
	private final RpcClient rpcClient;
	
	public RpcInvocationHandler(RpcClient rpcClient)
	{
		this.rpcClient = rpcClient;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
	{
		String service = method.getDeclaringClass().getName();
		RpcRequest request = new RpcRequest();
		request.setRequestId(UUID.generator());
		request.setService(service);
		request.setMethod(method.getName());
		request.setParams(args);
		RpcClientFuture future = rpcClient.invoke(request);
		if (future == null)
			return null;
		
		return future.get();
	}
}
